package com.noble.library.camera.dialog;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.noble.library.R;
import com.noble.library.camera.ImgSelectUtil;
import com.noble.library.view.base.BaseDialogFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * 拍照选择弹框
 */
public class ImgSelectDialog extends BaseDialogFragment {

    private Activity activity;
    private ImgSelectUtil.OnChooseCompleteListener listener;

    private ListView listView;
    private ArrayAdapter<String> mAdapter;
    private List<String> list = new ArrayList<>();

    public ImgSelectDialog() {
        settWindowAnimations(R.style.AnimDownInDownOutOverShoot);
        setGravity(Gravity.BOTTOM);
        setDialogSizeRatio(-2, -2);
    }

    public void setParam(Activity activity, ImgSelectUtil.OnChooseCompleteListener listener) {
        this.activity = activity;
        this.listener = listener;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        setContentView(R.layout.dialog_img_select);
        initView();
        return getContentView();
    }

    private void initView() {
        listView = (ListView) findViewById(R.id.list_view);

        findViewById(R.id.de_cancel).setOnClickListener(view -> dismiss());
        initListItem();
    }

    /**
     * 弹框弹窗条目内容
     */
    private void initListItem() {
        list.clear();
        list.add("拍照");
        list.add("从相册选择");

        mAdapter = new ArrayAdapter<>(getContext(), R.layout.dialog_img_item, R.id.de_type, list);
        listView.setAdapter(mAdapter);
        listView.setOnItemClickListener((adapterView, view, i, l) -> {
            if (i == 0) {  // 拍照
                ImgSelectUtil.getInstance().takePhoto(activity, listener);
            }

            if (i == 1) {   // 相册
                ImgSelectUtil.getInstance().pickPhoto(activity, listener);
            }
            dismiss();
        });
    }
}
